﻿using System;
using System.Globalization;
using System.Threading;
using System.Web;
using System.Web.UI.WebControls;

using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Users;




namespace DotNetNuke.Modules.UserDefinedTable
{


  public class EditTime : EditControl
  {

    private bool ConvertTime;
    private TextBox ctlTime;




    //public EditTime()
    //{
    //  base.Init += new EventHandler(this.EditTime_Init);
    //}



    protected override void OnInit(EventArgs e)
    {
      base.OnInit(e);
      //base.Init += new EventHandler(this.EditTime_Init);

      this.ctlTime = new TextBox();
      this.ctlTime.ID = EditControl.CleanID(string.Format("{0}_time", this.FieldTitle));
      this.ctlTime.CssClass = "NormalTextBox";
      Unit unit2 = new Unit("5em");
      this.ctlTime.Width = unit2;
      this.ctlTime.Attributes.Add("style", "text-align:right");

      if (!string.IsNullOrEmpty(this.Style))
        this.ctlTime.Style.Value = this.Style;

      this.Controls.Add(this.ctlTime);
      this.ConvertTime = this.NormalizeFlag;
      this.Value = this.DefaultValue;
    }




    //private void EditTime_Init(object sender, EventArgs evt)
    //{
    //  this.ctlTime = new TextBox();
    //  this.ctlTime.ID = EditControl.CleanID(string.Format("{0}_time", this.FieldTitle));
    //  this.ctlTime.CssClass = "NormalTextBox";
    //  Unit unit2 = new Unit("5em");
    //  this.ctlTime.Width = unit2;
    //  this.ctlTime.Attributes.Add("style", "text-align:right");

    //  if (!string.IsNullOrEmpty(this.Style))
    //    this.ctlTime.Style.Value = this.Style;
      
    //  this.Controls.Add(this.ctlTime);
    //  this.ConvertTime = this.NormalizeFlag;
    //  this.Value = this.DefaultValue;
    //}





    protected override bool IsValidType()
    {
      bool flag;
      string objField = this.ctlTime.Text.Trim();

      if (Null.IsNull(objField))
        return true;
      
      try
      {
        DateTime.ParseExact(objField, new string[] { "t", "T" }, Thread.CurrentThread.CurrentCulture, DateTimeStyles.None);
        flag = true;
      }
      catch (Exception e)
      {
        Exception exception = e;
        flag = false;
      }
      return flag;
    }





    public override string Value
    {
      get
      {
        DateTime dt;

        string expression = this.ctlTime.Text.Trim();

        if ( !string.IsNullOrEmpty(expression) )
        {
          if (!DateTime.TryParse(expression, out dt))
            return expression;

          //DateTime dt = DateTime.Parse(expression);
          if (this.ConvertTime && System.Web.HttpContext.Current.Request.IsAuthenticated)
          {
            UserInfo currentUserInfo = UserController.GetCurrentUserInfo();
            dt = new UserTime().ConvertToServerTime(dt, (double)currentUserInfo.Profile.TimeZone);
          }
          return DateTime.MinValue.AddHours((double)dt.Hour).AddMinutes((double)dt.Minute).AddSeconds((double)dt.Second).ToString("s");
        }
        return Null.NullString;
      }
      set
      {
        if ( string.IsNullOrEmpty(value) )
          this.ctlTime.Text = string.Empty;

        else
        {
          DateTime dt = DateTime.Parse(value);
          if (this.ConvertTime && System.Web.HttpContext.Current.Request.IsAuthenticated)
          {
            UserInfo currentUserInfo = UserController.GetCurrentUserInfo();
            dt = new UserTime().ConvertToUserTime(dt, (double)currentUserInfo.Profile.TimeZone);
          }
          this.ctlTime.Text = dt.ToString("t");
        }
      }
    }


  }

}

